/***************************************************************************
 *   File FilterTransfert.cpp		author belot nicolas (karrakis)    *
 *   define Class Filter                                                   *
 *   a base class for filter with Tranfert function                        *
 ***************************************************************************
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "FilterTransfert.h"
namespace libtrckr {

string FilterTransfert::HowToTune(){
	return "unset";
}

FilterTransfert::FilterTransfert(){
	_y = DynArray<double>(1,0.0);
}

FilterTransfert::FilterTransfert( const FilterTransfert& t):_y(t._y){
	//Transfert(t);
	setAs(t.getAs());
	setBs(t.getBs());
}

FilterTransfert::FilterTransfert(const Transfert&t){
	setAs(t.getAs());
	setBs(t.getBs());
	_y = DynArray<double>(t.getBs().size(),.0);
}

void FilterTransfert::setY(DynArray<double> Y){
	_y = DynArray<double>(Y.size(),0.);
	for(uint i=0;i<Y.size();i++){
		_y[i]=Y[i];
	}
}

void FilterTransfert::Init( ){}

void FilterTransfert::Tune(DynArray<int>){}


double* FilterTransfert::Preview(double* buffer, int size){
	DynArray<double>a = getAs();
	DynArray<double>b = getBs();
	double d;
	for(int i=0;i<size;i++){
		d=0.0;
		for(unsigned int j=0;j<a.size();j++){
			d+= a[i] * buffer[i-j];
		}
		
		for(unsigned int j=1;j<b.size();j++){
			d+= b[i] * _y[j];
		}
		
		for(unsigned int j=b.size()-1;j>0;j--){
			_y[i]=_y[i-1];
		}

		_y[1] = d;
		buffer[i]=d;
	}
	return buffer;
}

Buffer& FilterTransfert::Preview(Buffer &buf){
	double *buffer=buf.getContent();
	DynArray<double>a = getAs();
	DynArray<double>b = getBs();
	double d;
	/*for(unsigned int i=0;i<a.size();i++){
		printf("a%d = %g", i, a[i]);
	}

	for(unsigned int i=0;i<b.size();i++){
		printf("b%d = %g", i, b[i]);
	}*/

	for(unsigned int i=0;i<buf.size();i++){
		d=0.0;
		for(unsigned int j=0;j<a.size();j++){
			if(i-j>=0)
				d+= a[j] * buffer[i-j];
		}
		
		for(unsigned int j=1;j<b.size();j++){
			d+= b[j] * _y[j];
		}
		
		for(unsigned int j=b.size()-1;j>0;j--){
			_y[j]=_y[j-1];
		}
		_y[1] = d;
		//printf("%g => %g ", buffer[i],d);
		buffer[i]=d;

	}
			
	return buf;
}


double* FilterTransfert::Execution(double *buffer, int size){
	return Preview(buffer, size);
}

Buffer& FilterTransfert::Execution(Buffer &b){
	return Preview(b);
}
}

